<?php
namespace app\logic;

use app\common\corelib\CacheHelper;
use app\models\Area;
use app\models\Manager;
use app\models\Shop;
use app\models\ShopPlay;
use app\models\Tag;
use app\models\User;
use yii\db\Query;



class ShopLogic
{

    public function getShopPlay($shop_id){
        return ShopPlay::find()->where(['shop_id'=>$shop_id])->asArray()->all();
    }

    public function getAllShop(){
        return Shop::find()->asArray()->all();
    }
    public function getShopList($region_code,$tag_id=0/*,$skip=0,$limit=20*/)
    {
        if($tag_id){
            if(strlen($region_code)==6){
                return Shop::find()
                    ->from('{{%shop}} s')
                    ->leftJoin('{{%shop_tag}} st','s.shop_id=st.shop_id')
                    ->leftJoin('{{%tag}} t','t.tag_id=st.tag_id')
                    ->where("s.district_id=$region_code AND st.tag_id=$tag_id AND s.status=1 AND t.status=1")
                    //->offset($skip)
                    //->limit($limit)
                    ->asArray()
                    ->all();
            }else{
                return Shop::find()
                    ->from('{{%shop}} s')
                    ->leftJoin('{{%shop_tag}} st','s.shop_id=st.shop_id')
                    ->leftJoin('{{%tag}} t','t.tag_id=st.tag_id')
                    ->where("s.street_id=$region_code AND st.tag_id=$tag_id AND s.status=1 AND t.status=1")
                    //->offset($skip)
                    //->limit($limit)
                    ->asArray()
                    ->all();
            }
        }else{
            if(strlen($region_code)==6) {
                return Shop::find()
                    ->from('{{%shop}} s')
                    ->where("s.district_id=$region_code AND s.status=1")
                    //->offset($skip)
                    //->limit($limit)
                    ->asArray()
                    ->all();
            }else{
                return Shop::find()
                    ->from('{{%shop}} s')
                    ->where("s.street_id=$region_code AND s.status=1")
                    //->offset($skip)
                    //->limit($limit)
                    ->asArray()
                    ->all();
            }
        }
    }

    public function getTag($shop_id){
        return Tag::find()
            ->from('{{%tag}} t')
            ->leftJoin('{{%shop_tag}} st','t.tag_id=st.tag_id')
            ->where("st.shop_id=$shop_id AND t.status=1")
            ->asArray()
            ->all();
    }

    public function getShop($shop_id){
        return Shop::find()
            ->select('s.*,u.phone,m.manager_name')
            ->from('{{%shop}} s')
            ->leftJoin('{{%manager}} m','m.shop_id=s.shop_id AND m.on_work=1')
            ->leftJoin('{{%user}} u','u.user_id=m.user_id')
            ->where("s.shop_id=$shop_id")
            ->asArray()
            ->one();
    }

    public function getManager($shop_id){
        return Manager::find()
            ->where("shop_id=$shop_id AND on_work=1")
            ->asArray()
            ->one();
    }

    public function getShopByOwner($shop_id=0){
        return Shop::find()
            ->select('s.*')
            ->from('{{%shop}} s')
            ->where("s.shop_id=$shop_id")
            ->asArray()
            ->one();
    }

    public function getShopByManager($user_id=0){
        return Shop::find()
            ->select('s.*,m.manager_name')
            ->from('{{%shop}} s')
            ->leftJoin('{{%manager}} m','m.shop_id=s.shop_id')
            ->where("m.user_id=$user_id")
            ->asArray()
            ->one();
    }



    public function getManagerShop($user_id){
        return Shop::find()
            ->from('{{%shop}} s')
            ->leftJoin('{{%manager}} m','m.shop_id=s.shop_id AND m.on_work=1')
            ->where("m.user_id=$user_id")
            ->asArray()
            ->one();
    }

    public function getShopListByOwner($user_id){
        return Shop::find()
            ->select('s.*,a1.region_name as city,a2.region_name as district,a3.region_name as street')
            ->from('{{%shop}} s')
            ->leftJoin('{{%area}} a1','a1.region_code=s.city_id')
            ->leftJoin('{{%area}} a2','a2.region_code=s.district_id')
            ->leftJoin('{{%area}} a3','a3.region_code=s.street_id')
            ->where("s.owner_user_id=$user_id")
            ->asArray()
            ->all();
    }

    public function getManagerList($shop_id){
        return Manager::find()
            ->select('m.*,u.phone')
            ->from('{{%manager}} m')
            ->leftJoin('{{%user}} u','u.user_id=m.user_id')
            ->where(['shop_id'=>$shop_id])
            ->asArray()
            ->all();
    }


}
